package com.example.competitionregistration.service.impl;

import com.example.competitionregistration.common.Result;
import com.example.competitionregistration.common.ErrorCode;
import com.example.competitionregistration.controller.dto.UserDto;
import com.example.competitionregistration.entity.InstructorAccount;
import com.example.competitionregistration.entity.InstructorInform;
import com.example.competitionregistration.entity.StudentAccount;
import com.example.competitionregistration.mapper.RegisterMapper;
import com.example.competitionregistration.service.RegisterService;
import com.example.competitionregistration.util.GenerateAccount;
import com.example.competitionregistration.util.SHA1Util;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;

import java.util.UUID;

/**
 * @Author QuanJingFang
 * @Description //TODO 注册业务层，进行校验及密码加密
 * @Date 2023/7/17 21:29
 **/
@Controller
public class RegisterServiceImpl implements RegisterService {
    @Resource
    private RegisterMapper registerMapper;

    /**
     * @return com.example.competitionregistration.entity.Result
     * @Author QuanJingFang
     * @Description //TODO 对学生注册数据进行校验，并验证用户是否存在，获取注册数据
     * @Date 2023/7/18 17:05
     * @Param [studentDto]
     **/
    @Override
    public Result register(UserDto userDto) {
        String checkOUt = "\\w{5,}";
        if (userDto.getPassword().matches(checkOUt) == false) {
            return Result.error(ErrorCode.VERIFICATION_ERROR.getCode(), ErrorCode.VERIFICATION_ERROR.getMsg());
        }
        if (userDto.getStatus().equals("student")) {
            String account = GenerateAccount.generateUniqueId();
            int studentNumber = registerMapper.countStudent();
            account += String.valueOf(studentNumber);
            userDto.setAccount(account);
            String password = SHA1Util.SHA1(userDto.getPassword());
            StudentAccount studentAccount = new StudentAccount();
            studentAccount.setAccount(userDto.getAccount());
            studentAccount.setPassword(password);
            studentAccount.setId(UUID.randomUUID().toString());
            studentAccount.setStatus(userDto.getStatus());
            registerMapper.studentRegister(studentAccount);
            return Result.success(userDto);
        }
        String account = GenerateAccount.generateUniqueId();
        int studentNumber = registerMapper.countTeacher();
        account += String.valueOf(studentNumber);
        userDto.setAccount(account);
        String password = SHA1Util.SHA1(userDto.getPassword());
        InstructorAccount instructorAccount = new InstructorAccount();
        instructorAccount.setAccount(userDto.getAccount());
        instructorAccount.setPassword(password);
        instructorAccount.setJobNumber(userDto.getJobNumber());
        instructorAccount.setId(UUID.randomUUID().toString());
        instructorAccount.setStatus(userDto.getStatus());
        InstructorInform instructorInform = new InstructorInform();
        instructorInform.setJobNumber(userDto.getJobNumber());
        InstructorInform findInstructor = registerMapper.findInstructorInform(instructorInform);
        if (findInstructor == null) {
            return Result.error(ErrorCode.JOB_NUMBER_ERROR.getCode(), ErrorCode.JOB_NUMBER_ERROR.getMsg());
        }
        registerMapper.teacherRegister(instructorAccount);
        return Result.success(userDto);
    }
}
